package com.miracle.面试题.链表操作;

public class 链表常见操作 {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.addNode(3);
        linkedList.addNode(1);
        linkedList.addNode(5);
        linkedList.addNode(4);
        linkedList.addNode(6);
        linkedList.addNode(2);

        //打印链表
        linkedList.printLink();

        /*//测试链表结点个数
        System.out.println("链表结点个数为：" + linkedList.length());

        //链表排序
        Node head = linkedList.linkSort();
        System.out.println("排序后的头结点为：" + head.data);
        linkedList.printLink();

        //去除重复结点
        linkedList.distinctLink();
        linkedList.printLink();

        //链表反转
        linkedList.reserveLink();
        linkedList.printLink();

        //倒序输出/遍历链表
        linkedList.reservePrt(linkedList.head);

        //返回链表的中间结点
        Node middleNode = linkedList.findMiddleNode();
        System.out.println("中间结点的数值为："+middleNode.data);

        //判断链表是否有环
        boolean isRinged = linkedList.isRinged();
        System.out.println("链表是否有环：" + isRinged);
        //将链表的最后一个结点指向头结点，制造有环的效果
        Node lastNode = linkedList.getLastNode();
        lastNode.next = linkedList.head;
        isRinged = linkedList.isRinged();
        System.out.println("链表是否有环：" + isRinged);

        //删除指定结点
        Node nk = linkedList.findReverNode(3);
        System.out.println(nk.data);
        linkedList.deleteSpecialNode(nk);
        linkedList.printLink();

        //链表是否相交
        //新链表
        LinkedList linkedList1 = new LinkedList();
        linkedList1.addNode(1);
        linkedList1.addNode(2);
        linkedList1.printLink();
        System.out.println("链表一和链表二是否相交"+linkedList.isCross(linkedList.head, linkedList1.head));
        //把第二个链表从第三个结点开始接在第二个链表的后面，制造相交的效果
        linkedList1.findNode(2).next = linkedList.findNode(3);
        linkedList1.printLink();
        System.out.println("链表一和链表二是否相交"+linkedList.isCross(linkedList.head, linkedList1.head));
        */

        //如果两个链表相交求链表相交的结点的值
        LinkedList linkedList1 = new LinkedList();
        linkedList1.addNode(1);
        linkedList1.addNode(2);
        linkedList1.findNode(2).next = linkedList.findNode(3);
        linkedList1.printLink();
        Node n = linkedList1.findFirstCrossPoint(linkedList, linkedList1);
        if(n == null){
            System.out.println("链表不相交");
        }else{
            System.out.println("两个链表相交，第一个交点的数值为：" + n.data);
        }
    }
}



